package ssn.common.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import egovframework.rte.fdl.string.EgovDateUtil;
import egovframework.rte.fdl.string.EgovStringUtil;

public class DateUtil extends EgovDateUtil{

	public static String getDayOfWeekAsString(String yyyyMmDd) {
		if(yyyyMmDd == null){
			return null;
		}else if(yyyyMmDd.indexOf("-") < 0 && yyyyMmDd.split("-").length < 3){
			return null;
		}
		String yyyy = yyyyMmDd.split("-")[0];
		String mm = yyyyMmDd.split("-")[1];
		String dd = yyyyMmDd.split("-")[2];
        return getDayOfWeekAsString(yyyy, mm, dd);
    }
	
	public static String getDayOfAsString(String yyyyMmDd) {
		if(yyyyMmDd == null){
			return null;
		}else if(yyyyMmDd.indexOf("-") < 0 && yyyyMmDd.split("-").length < 3){
			return null;
		}
		String yyyy = yyyyMmDd.split("-")[0];
		String mm = yyyyMmDd.split("-")[1];
		String dd = yyyyMmDd.split("-")[2];
		return yyyy+"년 "+mm+"월 "+dd+"일";
	}
	
	public static String getCalcDateAsStringYyyyMmDd(String yyyyMmDd, int iTerm, String gubun) {
		if(yyyyMmDd == null){
			return "2013-01-01";
		}else if(yyyyMmDd.indexOf("-") < 0 && yyyyMmDd.split("-").length < 3){
			return "2013-01-01";
		}
        Calendar cd =
            new GregorianCalendar(Integer.parseInt(yyyyMmDd.split("-")[0]), Integer
                .parseInt(yyyyMmDd.split("-")[1]) - 1, Integer.parseInt(yyyyMmDd.split("-")[2]));

        cd.add(Calendar.DATE, iTerm);
        if (EgovStringUtil.equals(gubun, "day")) {
            cd.add(Calendar.DATE, iTerm);
        } else if (EgovStringUtil.equals(gubun, "month")) {
            cd.add(Calendar.MONTH, iTerm);
        } else if (EgovStringUtil.equals(gubun, "year")) {
            cd.add(Calendar.YEAR, iTerm);
        }
        String yyyy = EgovStringUtil.lPad(Integer.toString(cd.get(Calendar.YEAR)), 4,'0');
        String mm = EgovStringUtil.lPad(Integer.toString(cd.get(Calendar.MONTH) + 1), 2, '0');
        String dd = EgovStringUtil.lPad(Integer.toString(cd.get(Calendar.DAY_OF_MONTH)), 2, '0');
        

        return yyyy + "-" + mm + "-" + dd;
    }
	
	public static String getCalcDateAsStringYyyyMmDd(String yyyyMmDd, int iTerm) {
        return getCalcDateAsStringYyyyMmDd(yyyyMmDd, iTerm, "day");
    }
	
	/**
     * <p>
     * 현재 날짜를 년월일을 합쳐서 String으로 리턴하는 메소드 YYYY-MM-DD
     * </p>
     * @return 년+월+일 값
     */
    public static String getCDAsStringYYYY_MM_DD() {
        return getCurrentYearAsString() + "-" + getCurrentMonthAsString() + "-" + getCurrentDayAsString();
    }
    
    /**
     * <p>
     * 현재 날짜를 년월일을 합쳐서 String으로 리턴하는 메소드 YYYY-MM-DD
     * </p>
     * @return 년+월+일 값
     */
    public static String getDateReplaceString() {
    	return getCurrentYearAsString() + "_RESTRYYYY_" + getCurrentMonthAsString() + "_RESTRMM_" + getCurrentDayAsString()+"_RESTRDD_";
    }
    
    public static List<String> getYears(){
    	List<String> years = new ArrayList<String>();
    	int nowYear = getCurrentYearAsInt();
    	for(int i=2000;i<nowYear+1;i++){
    		years.add(""+i);
    	}
    	return years;
    }
    
    public static final List<String> getMonths(){
    	List<String> months = new ArrayList<String>();
    	for(int i=1;i<13;i++){
    		if(i<10){
    			months.add("0"+i);
    		}else{
    			months.add(""+i);
    		}
    	}
    	return months;
    }
}
